from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
import excel_info_sql
import biz_mysql

list1 = excel_info_sql.getConditions()


def Inovel_test():
    for i in range(0, len(list1)):
        # 小说名字
        Novel_name = list1[i]["Novel_name"]
        # 日期
        insert_time = list1[i]["date_time"]
        # 章节
        Chapter = list1[i]["Chapter"]
        # 推广名称
        Extension_name = list1[i]["Extension_name"]
        # 平台名称
        Platform = list1[i]["Platform"]
        # 判断平台
        if "掌都2" in Platform:
            wz = 'https://novel.zhangdu520.cn/notice/list'
            username = ''
        else:
            wz = 'https://novel.zhangdu520.com/default/login'
            username = ''
        driver = webdriver.Chrome()
        driver.get(wz)
        driver.maximize_window()
        driver.implicitly_wait(20)
        # 定位账号
        driver.find_element_by_xpath('//*[@id="username"]').clear()
        driver.find_element_by_xpath('//*[@id="username"]').send_keys('{0}'.format(username))
        # 定位密码
        driver.find_element_by_xpath('//*[@id="password"]').clear()
        driver.find_element_by_xpath('//*[@id="password"]').send_keys(password)
        # 点击登录
        driver.find_element_by_xpath('//*[@id="submit"]').click()
        # 点击小说推广
        try:
            WebDriverWait(driver, 60).until(
                EC.element_to_be_clickable((By.XPATH, '//li[7]//span'))
            )
            driver.find_element_by_xpath('//li[7]//span').click()
        except Exception:
            print("点击小说推广出错")
        # 点击全部
        try:
            WebDriverWait(driver, 60).until(
                EC.element_to_be_clickable((By.XPATH, '//div[1]/a[3]'))
            )
            driver.find_element_by_xpath('//div[1]/a[3]').click()
        except Exception:
            print("点击全部出错")
        # 点击搜索框
        try:

            WebDriverWait(driver, 60).until(
                EC.element_to_be_clickable((By.XPATH, '//div[1]/input'))
            )
            driver.find_element_by_xpath('//div[1]/input').clear()
            driver.find_element_by_xpath('//div[1]/input').send_keys(Novel_name)
        except Exception:
            print("点击搜索框")
        # 点击查询
        try:

            WebDriverWait(driver, 60).until(
                EC.element_to_be_clickable((By.XPATH, '//div[2]/button'))
            )
            driver.find_element_by_xpath('//div[2]/button').click()
        except Exception:
            print("点击查询出错")
        # 判断是外推或内推
        if Chapter >= 3:
            # 点击创建外推
            try:
                time.sleep(1)
                WebDriverWait(driver, 60).until(
                    EC.element_to_be_clickable((By.XPATH, '//td[8]/a[1]'))
                )
                driver.find_element_by_xpath('//td[8]/a[1]').click()
            except Exception:
                print("点击创建外推出错")

            # 获取外推链接  Chapter 为推广章节
            try:
                time.sleep(0.5)
                driver.find_element_by_xpath('//tr[{0}]//a[1][text()="获取外推链接"]'.format(Chapter)).click()
            except Exception:
                print("获取外推链接出错")

            # 外推名称 Extension_name 为推广名称
            try:
                time.sleep(1)
                WebDriverWait(driver, 60).until(
                    EC.element_to_be_clickable((By.XPATH, '//div[1]/div/input'))
                )
                driver.find_element_by_xpath('//div[1]/div/input').clear()
                driver.find_element_by_xpath('//div[1]/div/input').send_keys(str(insert_time) + Extension_name)
            except Exception:
                print("外推名称出错")

            # 点击认证公众号
            try:
                time.sleep(1)
                WebDriverWait(driver, 60).until(
                    EC.element_to_be_clickable((By.XPATH, '//div[2]/div/label[1]/span'))
                )
                driver.find_element_by_xpath('//div[2]/div/label[1]/span').click()
            except Exception:
                print("点击认证公众号出错")
            # 点击保存创建
            driver.find_element_by_xpath('//button[text()="保存创建"]').click()
            # url
            try:
                WebDriverWait(driver, 60).until(
                    EC.element_to_be_clickable((By.XPATH, '//div[@class="text-success text-copy-url"]')))
                url = driver.find_element_by_xpath('//div[@class="text-success text-copy-url"]').text
                print(url)
                # 数据入库
                biz_mysql.insert_novel_mysql(url=url, Public_platform=Platform, Novel_name=Novel_name, Chapter=Chapter,
                                             datetime=insert_time, Extension_name=Extension_name)
            except Exception:
                print("获取url出错")
            # 取消
            try:
                time.sleep(1)
                driver.find_element_by_xpath('//div[@class="modal fade in"]//button/span[1][text()="×"]').click()
            except Exception:
                print("外推取消出错")
        else:
            # 点击创建内推
            try:

                WebDriverWait(driver, 60).until(
                    EC.element_to_be_clickable((By.XPATH, '//td[8]/a[2]'))
                )
                driver.find_element_by_xpath('//td[8]/a[2]').click()
            except Exception:
                print("点击创建内推出错")
            # 获取内推链接
            try:
                driver.find_element_by_xpath('//tr[{0}]//span[2]/a'.format(Chapter)).click()
            except Exception:
                print("获取内推链接出错")
            # 内推名称
            try:
                # time.sleep(0.5)
                WebDriverWait(driver, 60).until(
                    EC.element_to_be_clickable((By.XPATH, '//div/input'))
                )
                driver.find_element_by_xpath('//div/input').clear()
                driver.find_element_by_xpath('//div/input').send_keys(str(insert_time) + Extension_name)
            except Exception:
                print("内推名称出错")
            # 保存创建
            try:
                driver.find_element_by_xpath('//button[text()="保存创建"]').click()
            except Exception:
                print("保存创建出错")
            # url
            try:
                time.sleep(1)
                url = driver.find_element_by_xpath('//div[2][@class="text-success text-copy-url"]').text
                print(url)
                # 数据入库
                biz_mysql.insert_novel_mysql(url=url, Public_platform=Platform, Novel_name=Novel_name, Chapter=Chapter,
                                             datetime=insert_time, Extension_name=Extension_name)
            except Exception:
                print("数据入库出错")
            # 取消
            try:
                driver.find_element_by_xpath('//div[@class="modal fade in"]//button/span[1]').click()
            except Exception:
                print("内推取消出错")
        driver.quit()


if __name__ == '__main__':
    Inovel_test()
